package com.shelpe.services.sinbad.utils;

import java.util.ArrayList;
import java.util.List;

import com.shelpe.services.sinbad.service.simbaproxy.SegmentClient;

public class WordUtils {
	
	public static boolean compare(String word, String other){
		return (new Word(word)).equals(new Word(other));
	}
	
	public static String trim(String original){
		original = original.replaceAll(" ", "");
		return original;
	}
	
	public static String join(List<String> words){
		StringBuffer buffer = new StringBuffer();
		if(words != null){
			for(String word: words){
				buffer.append(word).append(" ");
			}
		}
		return buffer.length() > 1?buffer.substring(0, buffer.length() - 1):buffer.toString();
	}
	
	public static Builder build(){
		Builder builder = new Builder();
		return builder;
	}
	
	public static Builder build(List<String> words){
		Builder builder = WordUtils.build();
		for(String word: words){
			builder.add(word);
		}
		return builder;
	}
	
	public static class Builder{
		
		private List<Word> words = new ArrayList<Word>();
		public void add(String word){
			this.words.add(new Word(word));
		}
		
		public void add(SegmentClient.Segment segment){
			this.words.add(new Word(segment.getWord(), segment.getSegments()));
		}
		public boolean contain(String w){
			Word searcher = null;
			searcher = new Word(w);
			
			for(Word word: this.words){
				if(word.equals(searcher)){
					return true;
				}
			}
			return false;
		}
		public boolean contain(SegmentClient.Segment segment){
			Word searcher = null;
			searcher = new Word(segment.getWord(), segment.getSegments());
			
			for(Word word: this.words){
				if(word.equals(searcher)){
					return true;
				}
			}
			return false;
		}
		@Override
		public String toString() {
			StringBuffer buffer = new StringBuffer();
			if(this.words != null){
				for(Word word:this.words){
					buffer.append(word.toString()).append("\n");
				}
			}
			return buffer.toString();
		}
		
		
	}

	
	private static class Word{
		private String original;
		
		private List<String> cells;
		
		public Word(String w){
			this.original = w;
			String[] cells = w.split(" ");
			if(cells.length > 1){
				this.cells = new ArrayList<String>();
				for(String cell: cells){
					this.cells.add(cell);
				}
			}
		}
		
		public Word(String w, List<String> segments){
			this.original = w;
			if(segments != null){
				this.cells = segments;
			}
		}
		
		@SuppressWarnings("unused")
		private static List<String> combine(List<String> cells){
			List<String> words = new ArrayList<String>();
			if(cells.size() == 2){
				words.add(cells.get(0) + cells.get(1));
				words.add(cells.get(1) + cells.get(0));
			}else if(cells.size() > 2){
				for(String prefix: cells){
					List<String> toCombines = new ArrayList<String>();
					for(String cell: cells){
						if(!cell.equals(prefix)){
							toCombines.add(cell);
						}
					}
					List<String> others = Word.combine(toCombines);
					for(String other:others){
						words.add(prefix + other);
						words.add(other + prefix);
					}
				}
			}
			return words;
		}

		@Override
		public int hashCode() {
			return this.original.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			Word that = (Word) obj;
			boolean equal = this.cells.size() == that.cells.size();
			for(String cell: that.cells){
				equal = equal && this.cells.contains(cell);
			}
			return equal;
		}

		@Override
		public String toString() {
			StringBuffer buffer = new StringBuffer();
			buffer.append(this.original).append("-");
			if(this.cells != null){
				for(String cell:this.cells){
					buffer.append(cell).append(" ");
				}
			}
			return buffer.toString();
		}
		
		
	}
}
